package com.app.teaware.basis;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.teaware.bean.BaseListData;
import com.app.teaware.bean.BaseListDataListBean;
import com.app.teaware.bean.BaseListInfoListBean;
import com.app.teaware.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 信息列表类，传BaseListInfoListBean即可
 * */
public class BaseInfoListActivity extends BasisActivity {


    public static void toInfoList(Context mContext, BaseListInfoListBean listBean){
        Intent intent = new Intent(mContext,BaseInfoListActivity.class);
        intent.putExtra("list",listBean);
        mContext.startActivity(intent);
    }

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;

    private MyAdapterList mAdapter;
    private BaseListInfoListBean mListBean;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.base_recycler_with_top);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        setTitleLeftButton(null);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);


        mListBean = (BaseListInfoListBean) getIntent().getSerializableExtra("list");
        if(mListBean == null){
            showToast(R.string.app_data_empty);
            return;
        }

        setTitle(mListBean.getTitle());
        setListView();
    }


    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }


    private void setEmptyView() {
        if (mListBean == null || mListBean.getList().size() == 0) {
            findViewById(R.id.refresh_view).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.refresh_view).setVisibility(View.INVISIBLE);
        }
    }


    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {

        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.base_list_info_list_item, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            BaseListDataListBean data = mListBean.getList().get(position);
            viewHolder.myAdapterListChild.setData(position,data);
//            viewHolder.img_icon.setImageResource(data.getResid());
//            viewHolder.view_item.setBackgroundResource(data.getResBg());
//            viewHolder.list_item.setBackgroundResource(R.drawable.item_shadow_bluelight_selector);

        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.recyclerview)
            public RecyclerView recyclerview;

            public MyAdapterListChild myAdapterListChild;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);

                LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
                recyclerview.setLayoutManager(layoutManager);
                myAdapterListChild = new MyAdapterListChild(0,null);
                recyclerview.setAdapter(myAdapterListChild);
            }

        }
    }

    public class MyAdapterListChild extends RecyclerView.Adapter<MyAdapterListChild.ViewHolder> {

        BaseListDataListBean mData;
        int childPosition;
        public MyAdapterListChild(int position,BaseListDataListBean data) {
            this.childPosition = position;
            this.mData = data;
        }

        public  void setData( int position,BaseListDataListBean data) {
            this.childPosition = position;
            this.mData = data;
            notifyDataSetChanged();
        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.base_list_info_list_item_child, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            BaseListData data = mData.getList().get(position);
            viewHolder.text_name.setText(data.getName());
            viewHolder.text_value.setText( data.getId());

        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mData == null? 0 :mData.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.text_value)
            public TextView text_value;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }

}
